package com.tongfusoft.document.web.controller.administration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.document.entity.DocHistory;
import com.tongfusoft.document.entity.FieldsetHasMetas;
import com.tongfusoft.document.entity.InputProfiles;
import com.tongfusoft.document.entity.IpFieldsets;
import com.tongfusoft.document.entity.MetadataDefine;
import com.tongfusoft.document.entity.SystemParam;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.metadata.service.IMetadataService;
import com.tongfusoft.document.manager.profile.service.InputProfileService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.DataTable;
import com.tongfusoft.spring.web.core.data.ResultModel;
import com.tongfusoft.utils.JsonUtils;

@Controller
public class InputProfileController extends BaseController{
	@Autowired
	@Qualifier("profileService")
	public InputProfileService inputProfileService;
	@Autowired
	@Qualifier(value="metaDataManagerService")
	public IMetadataService metaDataManagerService;
	
	@RequestMapping("/manager/inputProfile")
	@UrlDesc(value = "模板列表页")
	public String template(HttpServletRequest request, Model model) {

		model.addAttribute("mainPageKey", "inputProfile");
		model.addAttribute("mainPage", "manager/inputProfile/inputProfile");

		return "base";
	}
	
	@RequestMapping("/inputProfile/query")
	@UrlDesc(value = "获得模板列表内容")
	@ResponseBody
	public DataTable<InputProfiles> inputProfilesQuery(Model model, int length, int start, int draw, String extra_search) {

		DataTable rm = new DataTable();
		try {		
			
			InputProfiles inputProfiles = (InputProfiles) JsonUtils.getObject4JsonString(extra_search, InputProfiles.class);
			int pageIndex = (start / length)+1;
			PageInfo pageInfo = this.inputProfileService.profileList(length, pageIndex, inputProfiles);
			
			rm.setDraw(draw);
			rm.setRecordsFiltered(pageInfo.getTotal());
			rm.setRecordsTotal(pageInfo.getTotal());
			rm.setData(pageInfo.getList());
		} catch (Exception e) {

			e.printStackTrace();
		}

		return rm;

	}
	
	@RequestMapping("manager/inputProfile/inputProfileDetail/{profileId}")
	@UrlDesc(value = "模板详情页面")
	public String detail(@PathVariable("profileId") String profileId, Model model) {
		model.addAttribute("profileId", profileId);
		model.addAttribute("mainPageKey", "inputProfileDetail");
		model.addAttribute("mainPage", "manager/inputProfile/inputProfileDetail");
		return "base";
	}
	
	
	@RequestMapping("/inputProfile/add")
	@UrlDesc(value = "添加模板")
	@ResponseBody
	public ResultModel add(InputProfiles inputProfiles) {

		ResultModel rm = new ResultModel();

		try {
			if(inputProfiles.getProfileName().equals("") || inputProfiles.getProfileName().equals(null)){
				rm.setState("error");
			}else{
				rm.setState("success");
				this.inputProfileService.createProfile(inputProfiles, (Users)super.getUser());
				rm.setData(inputProfiles.getProfileId());
			}
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	
	@RequestMapping("manager/inputProfile/fieldset/{profileId}")
	@UrlDesc(value = "跳转添加fieldset页面")
	public String addElement(@PathVariable("profileId") String profileId, Model model) {
		
		InputProfiles temp = new InputProfiles();
		temp.setProfileId(profileId);
		InputProfiles profiles = this.inputProfileService.getInputProfiles(temp);
		model.addAttribute("profiles", profiles);
		model.addAttribute("mainPageKey", "fieldset");
		model.addAttribute("mainPage", "manager/inputProfile/fieldset");

		return "base";
	}
	
	@RequestMapping("/inputProfile/del")
	@UrlDesc(value = "删除模板")
	@ResponseBody
	public ResultModel del(InputProfiles inputProfiles) {
		
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			
			this.inputProfileService.deleteProfile(inputProfiles, (Users)super.getUser());
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	
	@RequestMapping("/manager/inputProfile/updateInputProfile/{profileId}")
	@UrlDesc(value = "跳转修改模板页面")
	public String updateInputProfile(@PathVariable("profileId") String profileId, Model model) {
			
		model.addAttribute("profileId", profileId);
		model.addAttribute("mainPageKey", "updateInputProfile");
		model.addAttribute("mainPage", "manager/inputProfile/updateInputProfile");

		return "base";
	}
	
	@RequestMapping("/inputProfile/isEnable")
	@UrlDesc(value = "启用和关闭模板")
	@ResponseBody
	public ResultModel switchIsEnable(HttpServletRequest request, Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			String profileId = request.getParameter("profileId");
			String isEnable = request.getParameter("isEnable");
			
			InputProfiles profiles = new InputProfiles();
			profiles.setProfileId(profileId);
			
			this.inputProfileService.updateEnableProfile(profiles, isEnable ,(Users)super.getUser());
		} catch(ServiceException se){
			rm.setCode(se.getMessage()); 
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		
		return rm;

	}
	
	@RequestMapping("/inputProfileDetail/fieldset/query")
	@UrlDesc(value = "获得详情页面fieldset列表")
	@ResponseBody
	public ResultModel detailFieldSetList(HttpServletRequest request) {
		
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			String profileId =request.getParameter("profileId");
			
			IpFieldsets fieldset = new IpFieldsets();
			fieldset.setProfileId(profileId);
			
			List<IpFieldsets> fieldsetsList = this.inputProfileService.FieldSetList(fieldset);
			
			rm.setData(fieldsetsList);
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}
	
	@RequestMapping("/fieldSetHasMetadata/query")
	@UrlDesc(value = "查询fieldset有的元数据")
	@ResponseBody
	public ResultModel fieldSetIsMetadata(HttpServletRequest request, Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			String fieldsetId = request.getParameter("fieldsetId");
			
			IpFieldsets ipFieldsets = new IpFieldsets();
			ipFieldsets.setFieldsetId(fieldsetId);
			List<MetadataDefine> defines = this.inputProfileService.fieldsetHasMetadata(ipFieldsets);
			
			rm.setData(defines);
		} catch(ServiceException se){
			rm.setCode(se.getMessage()); 
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	
	@RequestMapping("/inputProfile/detailData/{profileId}")
	@UrlDesc(value = "模板详情数据")
	@ResponseBody
	public ResultModel viewInputProfileDetail(@PathVariable("profileId") String profileId) {
		ResultModel rm = new ResultModel();
		rm.setState("success");	
		try {
			
			InputProfiles profiles = new InputProfiles();
			profiles.setProfileId(profileId);
			rm.setData(this.inputProfileService.getInputProfiles(profiles));

		} catch (Exception e) {
			e.printStackTrace();
			rm.setState("error");
		}

		return rm;

	}
	
	@RequestMapping("/fieldset/add")
	@UrlDesc(value = "添加Fielset")
	@ResponseBody
	public ResultModel addFielset(HttpServletRequest request,IpFieldsets ipFieldsets) {

		ResultModel rm = new ResultModel();

		try {
			if(ipFieldsets.getFieldsetName().equals("") || ipFieldsets.getFieldsetName().equals(null)){
				rm.setState("error");
			}else{
				rm.setState("success");
				
				String profileId = request.getParameter("profileId");
				ipFieldsets.setProfileId(profileId);
				this.inputProfileService.addFieldSets(ipFieldsets, (Users)super.getUser());
				rm.setData(ipFieldsets);
			}
			
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}
	
	@RequestMapping("/manager/inputProfile/fieldsetMetadata/{fieldsetId}/{opr}")
	@UrlDesc(value = "跳转到向fieldSet添加元数据页面")
	public String addFieldSet(@PathVariable("fieldsetId") String fieldsetId,@PathVariable("opr") String opr, Model model) {
		IpFieldsets ipFieldsets = new IpFieldsets();
		ipFieldsets.setFieldsetId(fieldsetId);
		IpFieldsets fieldsets = this.inputProfileService.getIpFieldsets(ipFieldsets);
		
		InputProfiles profiles = this.inputProfileService.getInputProfilesByFieldSetId(ipFieldsets);
		
		model.addAttribute("opr", opr);
		model.addAttribute("profiles", profiles);
		model.addAttribute("fieldsets", fieldsets);
		model.addAttribute("mainPageKey", "fieldsetMetadata");
		model.addAttribute("mainPage", "manager/inputProfile/fieldsetMetadata");

		return "base";
	}

	@RequestMapping("/fieldset/query")
	@UrlDesc(value = "获取fieldset列表")
	@ResponseBody
	public ResultModel fieldSetQuery(HttpServletRequest request, Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			String profileId = request.getParameter("profileId");
			IpFieldsets fieldset = new IpFieldsets();
			fieldset.setProfileId(profileId);
			rm.setData(this.inputProfileService.FieldSetList(fieldset));
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;

	}
	
	@RequestMapping("/fieldset/del")
	@UrlDesc(value = "删除fieldset")
	@ResponseBody
	public ResultModel fieldSetDel(IpFieldsets ipFieldsets) {
		
		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			FieldsetHasMetas fieldsetHas = new FieldsetHasMetas();
			this.inputProfileService.deleteFieldSets(ipFieldsets, (Users)super.getUser());		
			
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	
	@RequestMapping("/fieldsetMetadata/metadata/query")
	@UrlDesc(value = "获得元数据列表内容")
	@ResponseBody
	public ResultModel metadataDate(HttpServletRequest request, Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			String profileId = request.getParameter("profileId");
			InputProfiles profiles = new InputProfiles();
			profiles.setProfileId(profileId);
			List<MetadataDefine> defines = this.inputProfileService.metaDataList(profiles);
			
			rm.setData(defines);
		} catch(ServiceException se){
			rm.setCode(se.getMessage()); 
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	
	@RequestMapping("/fieldsetMetadata/add")
	@UrlDesc(value = "添加FielsetMetadata")
	@ResponseBody
	public ResultModel addFieldSetMetada(HttpServletRequest request,IpFieldsets ipFieldsets,FieldsetHasMetas fMetas) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		
		try {
			String fieldsetId = request.getParameter("fieldsetId");
			
			String metadataIdList = request.getParameter("metadataIdList");
			String metaSortList = request.getParameter("metaSortList");
			String isRequireList = request.getParameter("isRequireList");
			
			this.inputProfileService.addFieldSetMetada(metadataIdList, metaSortList, isRequireList,fieldsetId ,(Users)super.getUser());   
	
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;

	}
	
	@RequestMapping("/updateInputProfile/update")
	@UrlDesc(value = "修改模板")
	@ResponseBody
	public ResultModel updateInputProfile(HttpServletRequest request,InputProfiles inputProfiles) {

		ResultModel rm = new ResultModel();

		try {
			if(inputProfiles.getProfileName().equals("") || inputProfiles.getProfileName().equals(null)){
				rm.setState("error");
			}else{
				rm.setState("success");
				
				String profileId =request.getParameter("profileId");
				this.inputProfileService.updateProfile(profileId, inputProfiles, (Users)super.getUser());
				rm.setData(inputProfiles.getProfileId());
			}
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;

	}
	
	@RequestMapping("/manager/inputProfile/updateFieldsetHasMetadata/{fieldsetId}")
	@UrlDesc(value = "跳转到向fieldSet编辑元数据页面")
	public String updateMetaDataToFieldSet(@PathVariable("fieldsetId") String fieldsetId, Model model) {
		
		IpFieldsets ipFieldsets = new IpFieldsets();
		ipFieldsets.setFieldsetId(fieldsetId);
		IpFieldsets fieldsets = this.inputProfileService.getIpFieldsets(ipFieldsets);
		
		InputProfiles profiles = this.inputProfileService.getInputProfilesByFieldSetId(ipFieldsets);
		model.addAttribute("profiles", profiles);
		model.addAttribute("fieldsets", fieldsets);
		model.addAttribute("mainPageKey", "updateFieldsetHasMetadata");
		model.addAttribute("mainPage", "manager/inputProfile/updateFieldsetHasMetadata");

		return "base";
	}
	
	@RequestMapping("/fieldSetHasMetadata/update")
	@UrlDesc(value = "编辑fieldset已有的元数据")
	@ResponseBody
	public ResultModel updateFieldSetMetadata(HttpServletRequest request,IpFieldsets ipFieldsets,FieldsetHasMetas fMetas) {

		ResultModel rm = new ResultModel();
		rm.setState("success");
		
		try {
			String fieldsetId =request.getParameter("fieldsetId");
			String metadataIdList =request.getParameter("metadataIdList");
			String metaSortList =request.getParameter("metaSortList");
			String isRequireList =request.getParameter("isRequireList");
			
			this.inputProfileService.updateFieldSetMetadata(metadataIdList, metaSortList, isRequireList,fieldsetId ,(Users)super.getUser());   
			
		} catch(ServiceException se){
			rm.setCode(se.getMessage());
			rm.setState("error");
		}catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;

	}	
	
	@RequestMapping("/fieldSetHasMetadata/del")
	@UrlDesc(value = "删除fieldset已有的元数据")
	@ResponseBody
	public ResultModel metadataDel(String metadataId,String fieldsetId) {
		
		ResultModel rm = new ResultModel();
		try {
			FieldsetHasMetas fieldsetHasMetas = new FieldsetHasMetas();
			fieldsetHasMetas.setFieldsetId(fieldsetId);
			fieldsetHasMetas.setMetadataId(metadataId);
			this.inputProfileService.deleteMetaDataToFieldSet(fieldsetHasMetas, (Users)super.getUser());
			rm.setState("success");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;
		
	}
	

	
	
	
}
